<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>books</title>
<script type="text/javascript" src="script/jquery.js"></script>
<!-- 引入queryCondition.jsp为此页面的所有"a"标签加个click事件,获取所有hidden的参数,并拼为添加到标签a的href中 -->
<%@ include file="/commons/queryCondition.jsp" %>
<script type="text/javascript">
	$(function(){
		/* 验证跳转页面值是否合法 */
		$(".pageSkip").click(function () {
			// $("#pageNo").change(function()  检测到id="pageNo"值变化后执行该函数,不再使用,改为点击跳转按钮
			
			/* 得到跳转的页码 */
			var pageNo = $("#pageNo").val();
			//去空格
			pageNo = $.trim(pageNo); 
			//正则判断验证 校验是否为数字
			var reg = /^\d+$/;
			if(!reg.test(pageNo)){
				$("#pageNo").val("");
				alert("输入的页码不合法");
				return;
			}
			
			// 判断输入的页码大小是否在合法范围
			var pageNo2 = parseInt(pageNo);
			if(pageNo2 < 1 || pageNo2 > parseInt("${bookpage.totalPageNumber}")){
				$("#pageNo").val("");
				alert("输入的页码超出范围");
				return;
			}
			
			// 页码跳转
			var serializeVal = $(":hidden").serialize();
			window.location.href = "bookServlet?method=getBooks&pageNo=" + pageNo2 + "&" + serializeVal;
		});
			
		$(".pageBack").click(function () {
			window.location.href = "bookServlet?method=getBooks";
		})
	})
	   

</script>
</head>
<body>
	<center>
		<c:if test="${param.title != null }">
			您已经将${param.title } 放入到购物车中 。
		</c:if>
		<br> <br>
		<c:if test="${!empty sessionScope.ShoppingCart.books }">
			您的购物车中有${sessionScope.ShoppingCart.bookNumber }本书，
			<a href="bookServlet?method=forwardPage&page=cart&pageNo=${bookpage.pageNo }">查看购物车</a>
		</c:if>
		<br> <br> 
		<form action="bookServlet?method=getBooks" method="post">
			price: <input type="text" size="1" name="minPrice" /> - 
			       <input type="text" size="1" name="maxPrice" /> 
			       <input type="submit" value="查找">
		</form>
		<br>
		<br>
		<!-- 商品列表 -->
		<table cellpadding="10">
			<c:forEach items="${bookpage.list }" var="book">
				<tr>
					<td>
					   <a href="bookServlet?method=getBook&pageNo=${bookpage.pageNo }&id=${book.id }">${book.title }</a><br> 
					   ${book.author }
					</td>
					<td>${book.price }</td>
					<td><a href="bookServlet?method=addToCart&pageNo=${bookpage.pageNo }&id=${book.id }&title=${book.title}">加入购物车</a></td>
				</tr>
			</c:forEach>
		</table>
		<br>
		<br>
		<!-- 翻页展示 -->
		共 ${bookpage.totalPageNumber } 页 &nbsp;&nbsp; 当前第${bookpage.pageNo }页
		&nbsp;&nbsp;
		<c:if test="${bookpage.hasPrev }">
			<a href="bookServlet?method=getBooks&pageNo=1">首页</a>
            &nbsp;&nbsp;
            <a href="bookServlet?method=getBooks&pageNo=${bookpage.prevPage }">上一页</a>
		</c:if>
		&nbsp;&nbsp;
		<c:if test="${bookpage.hasNext }">
			<a href="bookServlet?method=getBooks&pageNo=${bookpage.nextPage }">下一页</a>                                    
            &nbsp;&nbsp;
            <a href="bookServlet?method=getBooks&pageNo=${bookpage.totalPageNumber }">末页</a>
		</c:if>
		&nbsp;&nbsp; 
		转到 <input type="text" size="1" id="pageNo" /> 页 
        <input type="submit" value="跳转" onclick="pageSkip();" class="pageSkip" />
        &nbsp;&nbsp;&nbsp;
        <input type="submit" value="返回" class="pageBack" />

	</center>
</body>
</html>